﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataModel
{
    public class PropertyCollection : System.Collections.ObjectModel.ObservableCollection<Property>
    {
        public void Add(String key, String val) {
            base.Add(new Property() { Key = key, Value = val });
        }

        /// <summary>
        /// Gets the keys.
        /// </summary>
        /// <value>
        /// The keys.
        /// </value>
        public IEnumerable<String> Keys
        {
            get
            {
                List<String> keys = new List<string>();
                foreach (Property p in this)
                {
                    keys.Add(p.Key);
                }
                return keys;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this instance has duplicate keys.
        /// </summary>
        /// <value>
        ///   <c>true</c> if this instance has dupes; otherwise, <c>false</c>.
        /// </value>
        public Boolean HasDupes
        {
            get
            {
                List<String> parsedKeys = new List<string>();
                foreach (Property p in this)
                {
                    if (parsedKeys.Contains(p.Key))
                    {
                        return true;
                    }
                    parsedKeys.Add(p.Key);
                }
                return false;
            }
        }

        /// <summary>
        /// Gets or sets the <see cref="System.String"/> with the specified key.
        /// </summary>
        /// <value>
        /// The <see cref="System.String"/>.
        /// </value>
        /// <param name="key">The key.</param>
        /// <returns>The value.</returns>
        public string this[string key]
        {
            get
            {
                return this.Single(p => p.Key == key).Value;
            }
            set 
            { 
                this.Single(p => p.Key == key).Value = value; 
            }
        }
    }
}
